﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using MammothMicro.SMTPGuard.Common;

namespace MammothMicro.SmtpGuardFilter.Filter
{
    ///<summary>
    /// Checks email against white list
    ///</summary>
    public class DomainWhiteListFilter:IEmailFilter
    {
        private List<string> _list = new List<string>();
        public bool Accept(MailMessage message)
        {
            return _list.Exists(p => p == Tools.GetDomainFromEmail(message.From.Address.ToLower()));
        }

        public void Reload()
        {
            var path = Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            path = path.Remove(0, 6);
            var reader = new StreamReader(path + @"\configuration\domainwhitelist.txt");
            var text = reader.ReadToEnd().ToLower();
            reader.Close();
            const string delimiter = "\r\n";
            var t = text.Split(delimiter.ToCharArray());
            _list = Tools.ClearEmptyItems(t.ToList());
        }
    }
}


